import asyncio, asyncpg
import traceback


async def main():
    connection = await asyncpg.connect(
        host="127.0.0.1",
        port="5432",
        database="products",
        user="around",
        password="zhouwei",
    )
    try:
        async with connection.transaction():
            await connection.execute("insert into brand values (DEFAULT, 'brand_1')")
            await connection.execute("insert into brand values (DEFAULT, 'brand_2')")
    except:
        print("插入错误：", traceback.format_exc())
    finally:
        query = """
            select * from brand where brand_name like 'brand%'
        """
        brands = await connection.fetch(query)
        print(brands)

        await connection.close()


asyncio.run(main())
